package web

import (
	assetfs "github.com/elazarl/go-bindata-assetfs"
	"github.com/gin-gonic/gin"
	"net/http"
	"server/vue"
)

type StaticRouter struct{}

func (s *StaticRouter) InitStaticRouter(Router *gin.RouterGroup) (R gin.IRoutes) {
	r := Router.Group("/")
	// 静态文件路径
	const staticPath = `vue/dist/`
	staticDir := assetfs.AssetFS{
		Asset:     vue.Asset,
		AssetDir:  vue.AssetDir,
		AssetInfo: nil,
		Prefix:    staticPath + "static/",
		Fallback:  "index.html",
	}
	// 加载静态文件
	r.StaticFS("/static", &staticDir)
	r.GET("/", func(c *gin.Context) {
		// 设置响应状态
		c.Writer.WriteHeader(http.StatusOK)
		// 载入首页
		indexHTML, _ := vue.Asset(staticPath + "index.html")
		c.Writer.Write(indexHTML)
		// 响应HTML类型
		c.Writer.Header().Add("Accept", "text/html")
		// 显示刷新
		c.Writer.Flush()
	})
	//// 关键点【解决页面刷新404的问题】
	//r.Any(func(c *gin.Context) {
	//	// 设置响应状态
	//	c.Writer.WriteHeader(http.StatusOK)
	//	// 载入首页
	//	indexHTML, _ := vue.Asset(staticPath + "index.html")
	//	c.Writer.Write(indexHTML)
	//	// 响应HTML类型
	//	c.Writer.Header().Add("Accept", "text/html")
	//	// 显示刷新
	//	c.Writer.Flush()
	//})
	return r
}
